#include "historymessagedialog.h"

#include <QPushButton>
#include"modle/data.h"
#include"debug.h"
using namespace std;


HistoryMessageDialog::HistoryMessageDialog(QWidget *parent)
    :QDialog(parent)
{
    layout=new QVBoxLayout();
    layout->setContentsMargins(0,0,0,0);
    layout->setSpacing(10);
    layout->setAlignment(Qt::AlignTop);

    this->setLayout(layout);
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setFixedSize(600,600);
    this->setWindowIcon(QIcon(":/Resources/windowIcon.png"));
    this->setWindowTitle("历史消息");
    // 设置对话框背景
    this->setStyleSheet(
        "QDialog {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:1,"
        "               stop:0 #1a1a2e, stop:0.5 #16213e, stop:1 #0d0d1a);"
        "   color: #e0e0e0;"
        "   border: 1px solid rgba(255, 255, 255, 0.1);"
        "   border-radius: 12px;"
        "}");

    keybut=new QRadioButton();
    keybut->setText("按关键词查询");
    keybut->setChecked(true);
    // 美化单选按钮
    keybut->setStyleSheet(
        "QRadioButton {"
        "   color: #e0e0e0;"
        "   font-size: 14px;"
        "   font-weight: bold;"
        "   spacing: 8px;"
        "   padding: 5px;"
        "}"
        "QRadioButton::indicator {"
        "   width: 16px;"
        "   height: 16px;"
        "   border-radius: 8px;"
        "   border: 2px solid #666;"
        "   background: rgba(255, 255, 255, 0.1);"
        "}"
        "QRadioButton::indicator:checked {"
        "   border: 2px solid #06d6a0;"
        "   background: qradialgradient(cx:0.5, cy:0.5, radius:0.4,"
        "               fx:0.5, fy:0.5, stop:0 #06d6a0, stop:1 transparent);"
        "}"
        "QRadioButton:hover {"
        "   color: #ffd166;"
        "}");
    timebut=new QRadioButton();
    timebut->setText("按时间查询");
    timebut->setStyleSheet(keybut->styleSheet());


    QHBoxLayout*radiolayout=new QHBoxLayout();
    radiolayout->setContentsMargins(0,0,0,0);
    radiolayout->setSpacing(5);
    radiolayout->setAlignment(Qt::AlignTop|Qt::AlignLeft);
    radiolayout->addWidget(keybut);
    radiolayout->addWidget(timebut);

    searchline=new QLineEdit();
    searchline->setPlaceholderText("🔍 要搜索的关键字...");
    searchline->setFixedHeight(45);
    searchline->setStyleSheet(
        "QLineEdit {"
        "   background: rgba(45, 45, 65, 0.8);"
        "   border: 2px solid rgba(255, 255, 255, 0.1);"
        "   border-radius: 22px;"
        "   color: #e0e0e0;"
        "   font-size: 14px;"
        "   padding: 12px 20px;"
        "   selection-background-color: rgba(255, 107, 107, 0.3);"
        "}"
        "QLineEdit:focus {"
        "   border: 2px solid rgba(255, 107, 107, 0.6);"
        "   background: rgba(55, 55, 75, 0.9);"
        "}"
        "QLineEdit::placeholder {"
        "   color: rgba(255, 255, 255, 0.5);"
        "   font-style: italic;"
        "}");

    searchbtn=new QPushButton();
    searchbtn->setFixedSize(45,45);
    searchbtn->setIcon(QIcon(":/Resources/search.png"));
    searchbtn->setIconSize(QSize(25,25));
    searchbtn->setStyleSheet(
        "QPushButton {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff6b6b, stop:1 #06d6a0);"
        "   border: none;"
        "   border-radius: 15px;"
        "}"
        "QPushButton:hover {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff8e8e, stop:1 #08e6b0);"
        "   transform: scale(1.05);"
        "}"
        "QPushButton:pressed {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff4b4b, stop:1 #04c690);"
        "}");



    QHBoxLayout *searchlayout=new QHBoxLayout();
    searchlayout->setSpacing(12);
    searchlayout->setContentsMargins(10,0,10,0);
    searchlayout->setAlignment(Qt::AlignTop|Qt::AlignLeft);
    searchlayout->addWidget(searchline);
    searchlayout->addWidget(searchbtn);



    //消息滚动区域
    scrollarea=new HistoryMessageShowArea();

    begintimelabel=new QLabel();
    begintimelabel->setText("开始时间");
    begintimelabel->setFixedHeight(40);
    begintimelabel->setStyleSheet(
        "QLabel {"
        "   color: #ffd166;"
        "   font-size: 14px;"
        "   font-weight: bold;"
        "   background: transparent;"
        "   padding: 8px 5px;"
        "}");
    begintimelabel->hide();

    begintime=new QDateTimeEdit();
    begintime->setFixedHeight(40);
    begintime=new QDateTimeEdit();
    begintime->setFixedHeight(40);
    begintime->setCalendarPopup(true);
    begintime->setStyleSheet(
        "QDateTimeEdit {"
        "   background: rgba(45, 45, 65, 0.8);"
        "   border: 2px solid rgba(255, 255, 255, 0.1);"
        "   border-radius: 8px;"
        "   color: #e0e0e0;"
        "   font-size: 14px;"
        "   padding: 5px 10px;"
        "   selection-background-color: rgba(255, 107, 107, 0.3);"
        "}"
        "QDateTimeEdit:focus {"
        "   border: 2px solid rgba(255, 107, 107, 0.6);"
        "   background: rgba(55, 55, 75, 0.9);"
        "}"
        "QDateTimeEdit::down-arrow, QDateTimeEdit::up-arrow {"
        "   background: transparent;"
        "   color: #06d6a0;"
        "}");
    begintime->hide();

    endtimelabel=new QLabel();
    endtimelabel->setText("结束时间");
    endtimelabel->setFixedHeight(40);
    endtimelabel->setStyleSheet(begintimelabel->styleSheet());
    endtimelabel->hide();

    endtime=new QDateTimeEdit();
    endtime->setFixedHeight(40);
    endtime->setCalendarPopup(true);
    endtime->setStyleSheet(begintime->styleSheet());
    endtime->hide();



    layout->addLayout(radiolayout);
    layout->addLayout(searchlayout);
    layout->addWidget(scrollarea);


    connect(keybut,&QRadioButton::clicked,this,[=](){

        scrollarea->hide();
        layout->removeWidget(scrollarea);

        begintimelabel->hide();
        begintime->hide();
        endtimelabel->hide();
        endtime->hide();
        searchbtn->hide();
        searchlayout->removeWidget(begintimelabel);
        searchlayout->removeWidget(begintime);
        searchlayout->removeWidget(endtimelabel);
        searchlayout->removeWidget(endtime);
        searchlayout->removeWidget(searchbtn);


        searchline->show();
        searchbtn->show();

        searchlayout->addWidget(searchline);
        searchlayout->addWidget(searchbtn);

        scrollarea->show();
        layout->addWidget(scrollarea);

    });

    connect(timebut,&QRadioButton::clicked,this,[=](){

        scrollarea->hide();
        layout->removeWidget(scrollarea);

        searchbtn->hide();
        searchline->hide();
        searchlayout->removeWidget(searchbtn);
        searchlayout->removeWidget(searchline);

        begintimelabel->show();
        begintime->show();
        endtimelabel->show();
        endtime->show();
        searchbtn->show();

        searchlayout->addWidget(begintimelabel);
        searchlayout->addWidget(begintime);
        searchlayout->addWidget(endtimelabel);
        searchlayout->addWidget(endtime,1);
        searchlayout->addWidget(searchbtn);

        scrollarea->show();
        layout->addWidget(scrollarea);
    });












#if TEST_UI
    module::User user;
    user.avatar=QIcon(":/Resources/avator1.jpg");
    QString content1="这是一条测试消息";
    QString content2="这是一条测试消息这是一条测试消息这是一条测试消息这是一条测试消息这是一条测试消息这是一条测试消息"
                       "这是一条测试消息这是一条测试消息这是一条测试消息这是一条测试消息"
                       "这是一条测试消息这是一条测试消息这是一条测试消息这是一条测试消息"
                       "这是一条测试消息这是一条测试消息这是一条测试消息";
    bool is=true;
    for(int i=0;i<2;i++)
    {
        user.name=QString::number(i+1);
        user.userId=QString::number(i+1);
        Message  mes;
        if(is)
        {
            mes=Message::makeMessage(MessageType::TEXT_TYPE,"1",user,content1.toUtf8(),"1");
        }
        else
        {
              mes=Message::makeMessage(MessageType::TEXT_TYPE,"1",user,content2.toUtf8(),"1");
        }
        is=!is;

        scrollarea->addItem(mes);
    }

#endif
}

HistoryMessageShowArea::HistoryMessageShowArea(QWidget *parent)
    :QScrollArea(parent)
{
    layout= new QVBoxLayout();
    layout->setContentsMargins(0,0,0,0);
    layout->setSpacing(10);
    layout->setAlignment(Qt::AlignTop);

    cotainer=new QWidget();
    cotainer->setLayout(layout);
    cotainer->setStyleSheet(
        "QWidget{"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:1,"
        "               stop:0 #1a1a2e, stop:0.5 #16213e, stop:1 #0d0d1a);"
        "   color: #e0e0e0;"
        "   border: 1px solid rgba(255, 255, 255, 0.1);"
        "   border-radius: 12px;"
        "}");

    this->setWidget(cotainer);
    this->setWidgetResizable(true);
    this->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

    // 美化滚动条
    this->verticalScrollBar()->setStyleSheet(
        "QScrollBar:vertical {"
        "   width: 10px;"
        "   background: rgba(255, 255, 255, 0.05);"
        "   border-radius: 5px;"
        "   margin: 2px;"
        "}"
        "QScrollBar::handle:vertical {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff6b6b, stop:1 #06d6a0);"
        "   border-radius: 5px;"
        "   min-height: 30px;"
        "}"
        "QScrollBar::handle:vertical:hover {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff8e8e, stop:1 #08e6b0);"
        "}"
        "QScrollBar::handle:vertical:pressed {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff4b4b, stop:1 #04c690);"
        "}"
        "QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {"
        "   border: none;"
        "   background: none;"
        "   height: 0px;"
        "}");

    this->horizontalScrollBar()->setStyleSheet(
        "QScrollBar:horizontal {"
        "   height: 8px;"
        "   background: rgba(255, 255, 255, 0.05);"
        "   border-radius: 4px;"
        "   margin: 2px;"
        "}"
        "QScrollBar::handle:horizontal {"
        "   background: qlineargradient(x1:0, y1:0, x2:0, y2:1,"
        "               stop:0 #ff6b6b, stop:1 #06d6a0);"
        "   border-radius: 4px;"
        "   min-width: 30px;"
        "}");

    this->setStyleSheet(
        "QScrollArea {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:1,"
        "               stop:0 #1a1a2e, stop:0.5 #16213e, stop:1 #0d0d1a);"
        "   color: #e0e0e0;"
        "   border: 1px solid rgba(255, 255, 255, 0.1);"
        "   border-radius: 12px;"
        "}");

}

void HistoryMessageShowArea::addItem(const Message &mes)
{
    HistoryItem* item= HistoryItem::makeMessageItem(this,mes);
    QVBoxLayout*lay=dynamic_cast<QVBoxLayout*>(cotainer->layout());
    lay->addWidget(item,0,Qt::AlignTop);
}

HistoryItem::HistoryItem(HistoryMessageShowArea *owner,const Message& mes, QWidget *parent)
    :QWidget(parent)
{
    layout=new QGridLayout();
    layout->setContentsMargins(10,10,10,10);
    layout->setSpacing(0);
    layout->setAlignment(Qt::AlignTop|Qt::AlignLeft);
    this->setLayout(layout);
    this->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    // 设置消息项背景
    this->setStyleSheet(
        "QWidget {"
        "   background: rgba(255, 255, 255, 0.05);"
        "   border: 1px solid rgba(255, 255, 255, 0.1);"
        "   border-radius: 12px;"
        "   margin: 2px;"
        "}"
        "QWidget:hover {"
        "   background: rgba(255, 255, 255, 0.1);"
        "   border: 1px solid rgba(255, 107, 107, 0.3);"
        "}");

    QPushButton*avator=new QPushButton();
    avator->setIcon(mes.sender.avatar);
    avator->setFixedSize(40,40);
    avator->setIconSize(QSize(35,35));
    avator->setStyleSheet(
        "QPushButton {"
        "   border: none;"
        "   border-radius: 10px;"
        "   background: qlineargradient(135deg, #ff6b6b, #ffa36c);"
        "}"
        "QPushButton:hover {"
        "   background: qlineargradient(135deg, #ff8e8e, #ffb78c);"
        "   border: 2px solid rgba(255, 255, 255, 0.3);"
        "}");

    QLabel*nameandtimelable=new QLabel();
    QString text=mes.sender.name+"|"+mes.time;
    nameandtimelable->setText(text);

    layout->addWidget(avator,0,0,2,1);
    layout->addWidget(nameandtimelable,0,1,1,1);


}

HistoryItem *HistoryItem::makeMessageItem(HistoryMessageShowArea*owner,const Message& mes)
{

    HistoryItem*ret=new  HistoryItem(owner,mes);
    QWidget*messcondent=nullptr;
    switch(mes.messageType)
    {
    case MessageType::FILE_TYPE:
        break;
    case MessageType::IMAGE_TYPE:
        break;
    case MessageType::TEXT_TYPE:
        messcondent=makeTextHistotyItem(mes);
        break;
    case MessageType::SPEECH_TYPE:
        break;
    default:
        LOG()<<"错误的消息类型";
        break;
    }

    ret->layout->addWidget(messcondent,1,1,6,1);
    return ret;
}

QWidget *HistoryItem::makeTextHistotyItem(const Message &mes)
{
    QLabel *showtext=new QLabel();
    showtext->setWordWrap(true);
    showtext->setText(QString().fromUtf8(mes.context));
    showtext->setStyleSheet(
        "QLabel {"
        "   color: #e0e0e0;"
        "   font-size: 14px;"
        "   background: transparent;"
        "   padding: 8px 5px;"
        "   line-height: 1.4;"
        "}");
    return showtext;
}

QWidget*HistoryItem::makeSpecchHistotyItem(const Message &mes)
{
    return nullptr;
}

QWidget *HistoryItem::makeImageHistotyItem(const Message &mes)
{
    return nullptr;
}

QWidget *HistoryItem::makeFileHistotyItem(const Message &mes)
{
    return nullptr;
}
